"""
自定义分页组件

def prettynum_show(request):

    # 1.根据自己的情况去筛选自己的数据
    queryset = models.PrettyNum.objects.filter(**data_dict).order_by('-level')

    # 2.实例化分页对象
    page_object = Pagination(request, queryset)

    context = {
        'search_data': search_data,
        'queryset': page_object.page_queryset,  # 分完页的数据
        'page_str': page_object.html()          # 生成页码
    }
    return render(request, 'prettynum_show.html', context)

在HTML页面中

{% for obj in queryset %}
    {{ obj.xx }}
{% endfor %}

<ul class='pagination'>
    {{ page_str }}
</ul>

"""
from django.utils.safestring import mark_safe


class Pagination(object):

    def __init__(self, request, queryset, page_size=10, page_param='page', plus=5):
        """
        :param request: 请求的对象
        :param queryset: 符合条件的数据（根据这个数据进行分页处理）
        :param page_size: 每页显示多少条数据
        :param page_param: 在URL中传递的获取分页的参数，例如：/?page=17
        :param plus: 显示当前页的 前或后几页（页码）
        """
        from django.http.request import QueryDict
        import copy
        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True
        self.query_dict = query_dict
        self.page_param = page_param

        page = request.GET.get(page_param, '1')
        if page.isdecimal():
            page = int(page)
        else:
            page = 1
        self.page = page
        self.page_size = page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size

        self.page_queryset = queryset[self.start:self.end]

        # 数据总条数
        total_count = queryset.count()
        # 页码总数
        total_page_count, div = divmod(total_count, page_size)
        if div:
            total_page_count += 1
        self.total_page_count = total_page_count
        self.plus = plus

    def html(self):
        # 计算出，显示当前页的前五页后五页
        if self.total_page_count < self.plus * 2 + 1:
            # 数据库中的数据比较少
            start_page = 1
            end_page = self.total_page_count + 1
        else:
            # 数据库中的数据比较多 > 11页
            if self.page <= self.plus:
                start_page = 1
                end_page = self.plus * 2 + 1
            elif self.page + self.plus > self.total_page_count:
                start_page = self.total_page_count - self.plus * 2
                end_page = self.total_page_count + 1
            else:
                start_page = self.page - self.plus
                end_page = self.page + self.plus + 1

        self.query_dict.setlist(self.page_param, [1])

        # 首页
        first = '<li><a href="?%s">首页</a></li>' % (self.query_dict.urlencode())
        # 上一页
        if self.page > 1:
            self.query_dict.setlist(self.page_param, [self.page - 1])
            prev = '<li><a href="?%s">上一页</a></li>' % (self.query_dict.urlencode())
        else:
            prev = '<li class="disabled"><a href="#">上一页</a></li>'
        # 页码
        page_list = [first, prev]
        for i in range(start_page, end_page):
            self.query_dict.setlist(self.page_param, [i])
            if i == self.page:
                ele = '<li class="active"><a href="?%s">%s</a></li>' % (self.query_dict.urlencode(), i)
            else:
                ele = '<li><a href="?%s">%s</a></li>' % (self.query_dict.urlencode(), i)
            page_list.append(ele)

        # 下一页
        if self.page < self.total_page_count:
            self.query_dict.setlist(self.page_param, [self.page + 1])
            nex = '<li><a href="?%s">下一页</a></li>' % (self.query_dict.urlencode())
        else:
            nex = '<li class="disabled"><a href="#">下一页</a></li>'
        page_list.append(nex)

        # 尾页
        self.query_dict.setlist(self.page_param, [self.total_page_count])
        last = '<li><a href="?%s">尾页</a></li>' % (self.query_dict.urlencode())
        page_list.append(last)

        page_str = mark_safe(''.join(page_list))
        return page_str